package br.com.pokertime.database;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	
	protected static SQLiteDatabase db;
	
	private static final String SCRIPT_DB_DELETE = "DROP TABLE IF EXISTS blinds";
	
	private static final String TAG = "DBAdapter";
	
	private static final String DATABASE_NAME = "pokerTimeDB";
	
	private static final int DATABASE_VERSION = 1;
	
	private DatabaseHelper DBHelper;
	
	private final Context context;
	
	private static final String SQL_TABELA_BLINDS = "CREATE TABLE blinds (bigBlind NUMERIC, smallBlind NUMERIC, nivel NUMERIC)";
	
	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(SQL_TABELA_BLINDS);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Atualizando banco da versao " + oldVersion + " para a versao "+newVersion);
			db.execSQL(SCRIPT_DB_DELETE);
			onCreate(db);
		}
	}
	
	public DBAdapter(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	// ---opens the database---
	public DBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	// ---closes the database---
	public void close() {
		DBHelper.close();
	}	
}
